package com.baizhi.service.impl;

import com.baizhi.dao.AdminDao;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.User;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.UUID;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminDao adminDao;
    @Override
    public Admin login(String name) {
        return adminDao.selectByName(name);
    }

    @Override
    public Admin selectById(Integer id) {
        return adminDao.selectById(id);
    }

    @Override
    public PageInfo<Admin> selectAll(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        PageInfo<Admin> pageInfo = new PageInfo<>(adminDao.selectAll());
        return pageInfo;
    }

    @Override
    public void insert(String username,String password) {
        String salt = UUID.randomUUID().toString().substring(0,4);
        System.out.println("随机盐值"+salt);
        Md5Hash md5Hash = new Md5Hash(password, salt, 1024);
        String result = md5Hash.toHex();
        Admin admin = new Admin();
        admin.setUsername(username);
// 密⽂存储
        admin.setPassword(result);
        admin.setSalt(salt);
        adminDao.insert(admin);
    }
}
